package com.example.demo_task_pool.task;

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.Random;

@Slf4j
@Component
public class Task {

    public static Random random=new Random();

    @Async("taskExecutor")
    public void taskOne() throws InterruptedException {
        log.info("开始任务1");
        long start=System.currentTimeMillis();
        Thread.sleep(random.nextInt(10000));
        long end=System.currentTimeMillis();
        log.info("完成任务1,耗时{}毫秒",end-start);
    }


    @Async("taskExecutor")
    public void taskTwo() throws InterruptedException {
        log.info("开始任务2");
        long start=System.currentTimeMillis();
        Thread.sleep(random.nextInt(10000));
        long end=System.currentTimeMillis();
        log.info("完成任务2,耗时{}毫秒",end-start);
    }

    @Async("taskExecutor")
    public void taskThree() throws InterruptedException {
        log.info("开始任务3");
        long start=System.currentTimeMillis();
        Thread.sleep(random.nextInt(10000));
        long end=System.currentTimeMillis();
        log.info("完成任务3,耗时{}毫秒",end-start);
    }

}
